    // Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import appConfig from "../../framework/appConfig";
import AudioManage, { soundList } from "../../framework/AudioManage";
import sceneMediator from "../../framework/sceneMediator";
import Tool from "../../framework/Tool";
import gameData from "../gameData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class levelViewMediator extends cc.Component {

    @property(cc.SpriteFrame)
    spriteFrameList : cc.SpriteFrame[] = [];

    @property(cc.Node)
    pageNode : cc.Node[] = [];

    @property(cc.Node)
    pageIndex : cc.Node = null;

    pageMax : number = 5;

    rank : number = 0;

    curPage = 0;

    changePage = false;

    levelData = null;

    start(){

        this.levelData = appConfig.levelRankLayer;

        this.scheduleOnce(()=>{
            this.createLevelView();
        },0.3)
    }

    createLevelView(){
        let maxLength = 0;
        if(this.rank == 0){
            maxLength = this.levelData[1] - this.levelData[0];
            this.pageMax = Math.ceil((this.levelData[1] - this.levelData[0]) / 10);
        }else if(this.rank == 1){
            maxLength = this.levelData[2] - this.levelData[1];
            this.pageMax = Math.ceil((this.levelData[2] - this.levelData[1]) / 10);
        }else{
            maxLength = 51 - this.levelData[2];
            this.pageMax = Math.ceil((51 - this.levelData[2]) / 10);
        }

        this.curPage = ((sceneMediator.getHighScene() - this.levelData[this.rank]) / 10) | 0;

        if(this.curPage >= this.pageMax){
            this.curPage = this.pageMax - 1;
        }

        for(let i in this.pageNode){
            let child =  this.pageNode[parseInt(i)].children;
            for(let j in child){

                let first = ((sceneMediator.getHighScene() - this.levelData[this.rank])  / 10) | 0;

                if(first >= this.pageMax){
                    first = this.pageMax - 1;
                }

                let index = (this.levelData[this.rank] - 1) + -10 + first * 10 + 10 * parseInt(i) + (parseInt(j) + 1);

                if(index > this.levelData[this.rank] - 1 + maxLength){
                    child[parseInt(j)].active = false;
                }else{
                    child[parseInt(j)].active = true;
                }

                if(index <= sceneMediator.getHighScene()){
                    child[parseInt(j)].getChildByName("close").active = false;
                    if(gameData.getLevelPumpkin()[index - 1] <= gameData.getProcessPumpkin()[index - 1]){
                        child[parseInt(j)].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[0];
                    }else {
                        child[parseInt(j)].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[1];
                    }
                }else if(index - sceneMediator.getHighScene() == 1){
                    child[parseInt(j)].getChildByName("close").active = false;
                    child[parseInt(j)].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[2];
                }else{
                    child[parseInt(j)].getChildByName("close").active = true;
                }
                child[parseInt(j)].getChildByName("num").getComponent(cc.Label).string = index + "";
                child[parseInt(j)].getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
            }
        }

        for(let i = 0 ; i < this.pageMax - 1; i++){
            let pgIndex = cc.instantiate(this.pageIndex.children[0]);
            this.pageIndex.addChild(pgIndex);
        }

        // console.log("翻页数",this.pageIndex, this.pageIndex.children);
        this.pageIndex.children[this.curPage].getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[3];
    }

    pageDown(event,data){
        if(this.changePage) return;
        if(this.curPage <= 0){
            console.log("到头了");
            return;
        }
        this.clickTimes = 0;
        this.changePage = true;
        this.pageIndex.children[this.curPage].getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[5];
        this.curPage--;
        this.pageIndex.children[this.curPage].getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[3];
        cc.tween(this.pageNode[0]).by(1,{
            position : cc.v2(800,0)
        }).start();
        cc.tween(this.pageNode[1]).by(1,{
            position : cc.v2(800,0)
        }).call(()=>{
            this.pageNode[2].x = -800;
            let children = this.pageNode[2].children;
            for(let i in children) {
                let index = (10 * (this.curPage - 1) + parseInt(i) + 1);
                if (index <= sceneMediator.getHighScene()) {
                    children[i].getChildByName("close").active = false;
                    if (gameData.getLevelPumpkin()[index - 1] && gameData.getLevelPumpkin()[index - 1] == gameData.getLevelPumpkin()[index - 1]) {
                        children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[0];
                    } else if (gameData.getLevelPumpkin()[index - 1] && gameData.getLevelPumpkin()[index - 1] < gameData.getLevelPumpkin()[index - 1]) {
                        children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[1];
                    }
                } else if (index - sceneMediator.getHighScene() == 1) {
                    children[i].getChildByName("close").active = false;
                    children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[2];
                }else{
                    children[i].getChildByName("close").active = true;
                }
                children[i].getChildByName("num").getComponent(cc.Label).string = index + "";
                children[i].getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
            }
            this.pageNode.unshift(this.pageNode.pop());
            this.changePage = false;
        }).start();
        console.log("左翻页");
    }

    clickTimes = 0;
    pageUp(event,data){
        if(this.changePage) return;
        if(this.curPage >= this.pageMax - 1){
            console.log("到头了");
            this.clickTimes++;
            if(this.clickTimes == 10){
                Tool.loadPrefab("prefabs/callDragon",res=>{
                    let view = cc.instantiate(res);
                    view.getComponent("callDragonMediator").setCallBack(false,()=>{

                    },()=>{
                        this.clickTimes = 0;
                    });
                    view.parent = cc.find("Canvas");
                })
            }
            return;
        }
        this.changePage = true;
        this.pageIndex.children[this.curPage].getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[5];
        this.curPage++;
        this.pageIndex.children[this.curPage].getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[3];
        cc.tween(this.pageNode[1]).by(1,{
            position : cc.v2(-800,0)
        }).start();
        cc.tween(this.pageNode[2]).by(1,{
            position : cc.v2(-800,0)
        }).call(()=>{
            this.pageNode[0].x = 800;
            let children = this.pageNode[0].children;
            for(let i in children){
                let index = (10 * (this.curPage +1) + parseInt(i) + 1);
                if (index <= sceneMediator.getHighScene()) {
                    children[i].getChildByName("close").active = false;
                    if (gameData.getLevelPumpkin()[index - 1] && gameData.getLevelPumpkin()[index - 1] == gameData.getLevelPumpkin()[index - 1]) {
                        children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[0];
                    } else if (gameData.getLevelPumpkin()[index - 1] && gameData.getLevelPumpkin()[index - 1] < gameData.getLevelPumpkin()[index - 1]) {
                        children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[1];
                    }
                } else if (index - sceneMediator.getHighScene() == 1) {
                    children[i].getChildByName("close").active = false;
                    children[i].getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.spriteFrameList[2];
                }else{
                    children[i].getChildByName("close").active = true;
                }
                children[i].getChildByName("num").getComponent(cc.Label).string = index + "";
                children[i].getComponent(cc.Button).clickEvents.forEach(value => value.customEventData = `${index}`);
            }
            this.pageNode.push(this.pageNode.shift());
            this.changePage = false;
        }).start();
        console.log("右翻页");
    }

    itemClick(event,data){
        AudioManage.playSFX(soundList.uiClick,false);
        sceneMediator.LoadToTargetScene(parseInt(data));
        // sceneMediator.showLevel = parseInt(data);
        AudioManage.stopBGM();
    }

    closeWindow(){
        // bannerControl.hideBanner(bannerType.left);
        this.node.destroy();
        if(this.callback != null){
            this.callback();
        }
    }

    callback = null;
    setCallBack(rank,callback){
        this.rank = rank;
        this.callback = callback;
    }


    // update (dt) {}
}
